package com.songcha.library_business.helper

import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.CSJAdError
import com.bytedance.sdk.openadsdk.CSJSplashAd
import com.bytedance.sdk.openadsdk.LocationProvider
import com.bytedance.sdk.openadsdk.TTAdConfig
import com.bytedance.sdk.openadsdk.TTAdConstant
import com.bytedance.sdk.openadsdk.TTAdDislike
import com.bytedance.sdk.openadsdk.TTAdLoadType
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTCustomController
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd.FullScreenVideoAdInteractionListener
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
import com.bytedance.sdk.openadsdk.TTNativeExpressAd.ExpressAdInteractionListener
import com.bytedance.sdk.openadsdk.TTRewardVideoAd
import com.bytedance.sdk.openadsdk.stub.activity.Stub_Activity
import com.bytedance.sdk.openadsdk.stub.activity.Stub_SingleTask_Activity
import com.bytedance.sdk.openadsdk.stub.activity.Stub_SingleTask_Activity_T
import com.bytedance.sdk.openadsdk.stub.activity.Stub_Standard_Activity
import com.bytedance.sdk.openadsdk.stub.activity.Stub_Standard_Activity_T
import com.bytedance.sdk.openadsdk.stub.activity.Stub_Standard_Landscape_Activity
import com.bytedance.sdk.openadsdk.stub.activity.Stub_Standard_Portrait_Activity
import com.qq.e.ads.banner2.UnifiedBannerADListener
import com.qq.e.ads.banner2.UnifiedBannerView
import com.qq.e.ads.cfg.VideoOption
import com.qq.e.ads.interstitial2.UnifiedInterstitialAD
import com.qq.e.ads.interstitial2.UnifiedInterstitialADListener
import com.qq.e.ads.nativ.ADSize
import com.qq.e.ads.nativ.NativeADUnifiedListener
import com.qq.e.ads.nativ.NativeExpressAD
import com.qq.e.ads.nativ.NativeExpressADView
import com.qq.e.ads.nativ.NativeUnifiedAD
import com.qq.e.ads.nativ.NativeUnifiedADData
import com.qq.e.ads.nativ.widget.NativeAdContainer
import com.qq.e.ads.rewardvideo.RewardVideoAD
import com.qq.e.ads.rewardvideo.RewardVideoADListener
import com.qq.e.ads.splash.SplashAD
import com.qq.e.ads.splash.SplashADListener
import com.qq.e.comm.managers.GDTAdSdk
import com.qq.e.comm.managers.setting.GlobalSetting
import com.qq.e.comm.util.AdError
import com.songcha.library_business.bean.advert.AdvertInfoBean
import com.songcha.library_business.config.VarConfig
import com.songcha.library_common.BuildConfig
import com.songcha.library_common.LibraryCommon
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.PackageUtil
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.px2dp
import com.ss.android.downloadlib.activity.JumpKllkActivity
import com.ss.android.downloadlib.activity.TTDelegateActivity
import com.ss.android.downloadlib.addownload.compliance.AppDetailInfoActivity
import com.ss.android.downloadlib.addownload.compliance.AppPrivacyPolicyActivity
import com.ss.android.socialbase.appdownloader.view.DownloadTaskDeleteActivity
import com.ss.android.socialbase.appdownloader.view.JumpUnknownSourceActivity
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.AutoSizeConfig
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit


class AdvertHelper {
    interface ITTAdSdkCallback<T>{
        fun onSuccess(wr:T)
        fun onFail(wr:T,po:Int,p1:String)
    }

    interface GDTAdSdkOnStartListener{
        fun onSuccess()
        fun onFail(e:Exception)
    }

    interface ISplashADListenerGDT{
        fun onSplashRenderFail(code:Int,msg:String)
        fun onADDismissed()
        fun onNoAD(code:Int,msg:String)
        fun onADPresent()
        fun onADClicked()
        fun onADTick(p0:Long)
        fun onADExposure()
        fun onADLoaded(p0:Long)
    }

    interface IRewardVideoADListenerGDT{
        fun onADLoad()
        fun onVideoCached()
        fun onADShow()
        fun onADExpose()
        fun onReward(p0: MutableMap<String, Any>)
        fun onADClick()
        fun onVideoComplete()
        fun onADClose()
        fun onError(code:Int,msg:String)
    }

    interface IUnifiedBannerADListenerGDT{
        fun onError(code:Int,msg:String)
        fun onNoAD(code:Int,msg:String)
        fun onADReceive()
        fun onADExposure()
        fun onADClosed()
        fun onADClicked()
        fun onADLeftApplication()
    }

    interface IGDTNativeADUnifiedListener{
        fun onNoAD(code:Int,msg:String)
        fun onADLoaded(icon:String,title:String,desc:String)
    }

    interface IGDTNativeExpressADListener{
        fun onNoAD(code:Int,msg:String)
        fun onADLoaded(adViews:List<View>)
        fun onRenderFail(adView:View)
        fun onRenderSuccess(adView:View)
        fun onADExposure(adView: View)
        fun onADClicked(adView: View)
        fun onADClosed(adView: View)
        fun onADLeftApplication(adView: View)
    }

    interface ICSJSplashAdListener{
        fun onSplashLoadSuccess()
        fun onSplashLoadFail(code:Int,msg:String)
        fun onSplashRenderSuccess()
        fun onSplashAdClose()
        fun onSplashRenderFail(code:Int,msg:String)
    }

    interface INativeExpressAdListener{
        fun onError(code:Int,msg:String)
        fun onNativeExpressAdLoad(adViews:List<Any>)
    }

    interface IAdvertRenderListener{
        fun onAdClicked(view:View,p1:Int)
        fun onAdShow(view:View,p1:Int)
        fun onRenderFail(p0: View, p1: String, p2: Int)
        fun onRenderSuccess(view: View, p1: Float, p2: Float)
        fun onDislikeReasonSelected(position: Int, value: String, enforce: Boolean)
    }

    interface IRewardVideoAdListener{
        fun onError(code: Int, msg: String)
        fun onVideoError()
        fun onRewardArrived(isRewardValid: Boolean, rewardType: Int, bundle: Bundle?)
        fun onRewardVideoCached()
        fun onClose()
    }

    interface IFeedAdListener{
        fun onError(code: Int, msg: String)
        fun onDislike()
        fun onRenderSuccess(adView:View)
    }

    interface IInsertScreenAdListener{
        fun onError(code: Int, msg: String)
        fun onSuccess()
        fun onAdShow()
        fun onAdClose()
    }

    interface ILoadAdvertCommonListener{
        fun onSuccess(type:Int,height:Int,view: View?)
        fun onError(type:Int,code:Int,msg:String)
        fun onClose(type:Int)
    }

    companion object{
        const val ADVERT_TYPE_SPLASH = 1
        const val ADVERT_TYPE_BANNER = 2
        const val ADVERT_TYPE_XXL = 3
        const val ADVERT_TYPE_REWARD = 4
        const val ADVERT_TYPE_INSERT_SCREEN = 5


        private var wrTTAdSdk:WeakReference<Any>?=null

        class TTAdSdkCallBack<T>(ref: T,val callback:ITTAdSdkCallback<T>):TTAdSdk.Callback{
            init {
                wrTTAdSdk = WeakReference(ref)
            }

            override fun success() {
                val wr = wrTTAdSdk?.get()?:return
                callback.onSuccess(wr as T)
            }

            override fun fail(p0: Int, p1: String) {
                LogUtil.log("广告sdk初始化失败")
                val wr = wrTTAdSdk?.get()?:return
                callback.onFail(wr as T,p0,p1)
            }

        }

        @JvmStatic
        fun <T> initAdvert(t: T, advertId:String, callback:ITTAdSdkCallback<T>){
            if(!isAdvertSupport()){
                callback.onFail(t,-1,"x86 cpu is not support")
                return
            }
            //autosize exclude advert page scale
            AutoSizeConfig.getInstance().externalAdaptManager
                .addCancelAdaptOfActivity(Stub_Activity::class.java)
                .addCancelAdaptOfActivity(Stub_SingleTask_Activity::class.java)
                .addCancelAdaptOfActivity(Stub_SingleTask_Activity_T::class.java)
                .addCancelAdaptOfActivity(Stub_Standard_Activity::class.java)
                .addCancelAdaptOfActivity(Stub_Standard_Activity_T::class.java)
                .addCancelAdaptOfActivity(Stub_Standard_Landscape_Activity::class.java)
                .addCancelAdaptOfActivity(Stub_Standard_Portrait_Activity::class.java)
                .addCancelAdaptOfActivity(AppPrivacyPolicyActivity::class.java)
                .addCancelAdaptOfActivity(AppDetailInfoActivity::class.java)
                .addCancelAdaptOfActivity(TTDelegateActivity::class.java)
                .addCancelAdaptOfActivity(JumpKllkActivity::class.java)
                .addCancelAdaptOfActivity(DownloadTaskDeleteActivity::class.java)
                .addCancelAdaptOfActivity(JumpUnknownSourceActivity::class.java)
            //advert
            TTAdSdk.init(
                LibraryCommon.getContext(),
                TTAdConfig.Builder()
                    .appId(advertId)
                    .useTextureView(true) //default use SurfaceView to play video,if has SurfaceView conflict，can use TextureView
                    .appName(PackageUtil.getAppName(t as Context))
                    .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
                    .allowShowNotify(true)
                    .debug(BuildConfig.DEBUG)
                    .directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI)
                    .supportMultiProcess(false)
                    .customController(object : TTCustomController() {
                        override fun isCanUsePhoneState(): Boolean {
                            return false
                        }

                        override fun isCanUseAndroidId(): Boolean {
                            return false
                        }

                        override fun isCanUseLocation(): Boolean {
                            return false
                        }

                        override fun isCanUsePermissionRecordAudio(): Boolean {
                            return false
                        }

                        override fun isCanUseWriteExternal(): Boolean {
                            return false
                        }

                        override fun isCanUseWifiState(): Boolean {
                            return false
                        }

                        override fun alist(): Boolean {
                            return false
                        }

                        override fun getAndroidId(): String? {
                            return null
                        }

                        override fun getDevImei(): String? {
                            return null
                        }

                        override fun getDevOaid(): String? {
                            return null
                        }

                        override fun getMacAddress(): String? {
                            return null
                        }

                        override fun getTTLocation(): LocationProvider? {
                            return null
                        }

                    })
                    .build())
            TTAdSdk.start(TTAdSdkCallBack(t,callback))

        }

        @JvmStatic
        fun initAdvert_GDT(context:Context,appid:String,callback:GDTAdSdkOnStartListener){
            val params: MutableMap<String, Boolean> = HashMap()
            params["soate"] = false
            GlobalSetting.setConvOptimizeInfo(params)

            GDTAdSdk.initWithoutStart(
                context,
                appid
            )

            GDTAdSdk.start(object : GDTAdSdk.OnStartListener {
                override fun onStartSuccess() {
                    callback.onSuccess()
                }

                override fun onStartFailed(e: Exception) {
                    callback.onFail(e)
                }
            })
        }

        @JvmStatic
        fun renderXxlAdView_GDT(adView:View){
            (adView as NativeExpressADView).render()
        }

        @JvmStatic
        fun loadXxlAdvert_GDT(activity: Activity,advertId:String,widthDp:Int,adCount:Int,callback:IGDTNativeExpressADListener) {
            if(!isAdvertSupport()){
                callback.onNoAD(-1,"x86 cpu is not support")
                return
            }
            val nativeExpressAD = NativeExpressAD(
                activity,
                ADSize(widthDp, ADSize.AUTO_HEIGHT),
                advertId,
                GDTNativeExpressADListener(activity,callback)
            ) // 传入Activity
            // 注意：如果您在平台上新建平台模板广告位时，选择了支持视频，那么可以进行个性化设置（可选）
            nativeExpressAD.setVideoOption(
                VideoOption.Builder()
                    .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频
                    .setAutoPlayMuted(true) // 自动播放时为静音
                    .build()
            ) //
            nativeExpressAD.loadAD(adCount)
        }


        class GDTNativeExpressADListener(val activity: Activity, val callback:IGDTNativeExpressADListener):NativeExpressAD.NativeExpressADListener{
            override fun onNoAD(p0: AdError?) {
                AutoSize.autoConvertDensityOfGlobal(activity)
                callback.onNoAD(p0!!.errorCode,p0!!.errorMsg)
            }

            override fun onADLoaded(adViews: MutableList<NativeExpressADView>?) {
                AutoSize.autoConvertDensityOfGlobal(activity)
                if(adViews.isNullOrEmpty()) return
                callback.onADLoaded(adViews)
            }

            override fun onRenderFail(p0: NativeExpressADView?) {
                callback.onRenderFail(p0!!)
            }

            override fun onRenderSuccess(p0: NativeExpressADView?) {
                if(p0 == null || !p0!!.isValid) {
                    callback.onRenderFail(p0!!)
                    return
                }
                callback.onRenderSuccess(p0!!)
            }

            override fun onADExposure(p0: NativeExpressADView?) {
                callback.onADExposure(p0!!)
            }

            override fun onADClicked(p0: NativeExpressADView?) {
                callback.onADClicked(p0!!)
            }

            override fun onADClosed(p0: NativeExpressADView?) {
                callback.onADClosed(p0!!)
            }

            override fun onADLeftApplication(p0: NativeExpressADView?) {
                callback.onADLeftApplication(p0!!)
            }

        }

        //private var wrTTAdCSJSplash:WeakReference<Any>? = null
        private var mSplashAd:CSJSplashAd? = null

        /*class TTAdCSJSplashAdListener<T>(ref: T,val listener:ICSJSplashAdListener<T>): TTAdNative.CSJSplashAdListener {
            init {
                wrTTAdCSJSplash = WeakReference(ref)
            }

            override fun onSplashLoadSuccess() {
                val wr = wrTTAdCSJSplash?.get()?:return
                listener.onSplashLoadSuccess(wr as T)
                //AutoSize.autoConvertDensityOfGlobal(splashActivity)
            }

            override fun onSplashLoadFail(p0: CSJAdError) {
                val wr = wrTTAdCSJSplash?.get()?:return
                listener.onSplashLoadFail(wr as T,p0.code,p0.msg)
                //AutoSize.autoConvertDensityOfGlobal(splashActivity)
                //splashActivity.toMain()
            }

            override fun onSplashRenderSuccess(ad: CSJSplashAd?) {
                val wr = wrTTAdCSJSplash?.get()?:return
                if (ad == null) {
                    return
                }
                mSplashAd = ad
                ad.setSplashAdListener(object : CSJSplashAd.SplashAdListener{
                    override fun onSplashAdShow(p0: CSJSplashAd?) {

                    }

                    override fun onSplashAdClick(p0: CSJSplashAd?) {

                    }

                    override fun onSplashAdClose(p0: CSJSplashAd?, p1: Int) {
                        listener.onSplashAdClose(wr as T)
                    }

                })

                listener.onSplashRenderSuccess(wr as T)

            }

            override fun onSplashRenderFail(p0: CSJSplashAd, p1: CSJAdError) {
                val wr = wrTTAdCSJSplash?.get()?:return
                listener.onSplashRenderFail(wr as T,p1.code,p1.msg)
            }

        }*/

        //private var mTTAdNative:TTAdNative? = null

        @JvmStatic
        fun createAdNative(context:Context):TTAdNative{
            return TTAdSdk.getAdManager().createAdNative(context)
            //if(mTTAdNative == null)
            //mTTAdNative = TTAdSdk.getAdManager().createAdNative(context)
        }

        private const val LOAD_AD_TIMEOUT=3500

        @JvmStatic
        fun loadSplashAd(activity: Activity,codeId:String,advertHeight:Int,isPreLoad: Boolean,listener:ICSJSplashAdListener){
            if(!isAdvertSupport()){
                listener.onSplashRenderFail(-1,"x86 cpu is not support")
                return
            }
            val ttAdNative = createAdNative(activity)
            val adSlot = AdSlot.Builder()
                .setCodeId(codeId)
                //if not distinguish render type,must need use setImageAcceptedSize(px) and setExpressViewAcceptedSize(dp) simultaneously,
                //different set may cause render exception
                .setImageAcceptedSize(ScreenUtil.getScreenWidth(),advertHeight)
                //.setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
                .setAdLoadType(if(isPreLoad) TTAdLoadType.PRELOAD else TTAdLoadType.LOAD)//recommend for mark this advert is preload(cache) or real time
                .build()
            ttAdNative.loadSplashAd(adSlot, object:TTAdNative.CSJSplashAdListener{
                override fun onSplashLoadSuccess() {
                    AutoSize.autoConvertDensityOfGlobal(activity)
                    listener.onSplashLoadSuccess()
                }

                override fun onSplashLoadFail(p0: CSJAdError?) {
                    AutoSize.autoConvertDensityOfGlobal(activity)
                    if(p0 == null) return
                    listener.onSplashRenderFail(p0.code,p0.msg)
                }

                override fun onSplashRenderSuccess(ad: CSJSplashAd) {
                    if (ad == null) {
                        return
                    }
                    mSplashAd = ad
                    ad.setSplashAdListener(object : CSJSplashAd.SplashAdListener{
                        override fun onSplashAdShow(p0: CSJSplashAd?) {

                        }

                        override fun onSplashAdClick(p0: CSJSplashAd?) {

                        }

                        override fun onSplashAdClose(p0: CSJSplashAd?, p1: Int) {
                            listener.onSplashAdClose()
                        }

                    })
                    listener.onSplashRenderSuccess()
                }

                override fun onSplashRenderFail(p0: CSJSplashAd?, p1: CSJAdError?) {
                    if(p1 == null) return
                    listener.onSplashRenderFail(p1.code,p1.msg)
                }

            }, LOAD_AD_TIMEOUT)
        }

        var mSplashADGDT:SplashAD? = null

        @JvmStatic
        fun loadSplashAd_GDT(activity:Activity,codeId:String,listener:ISplashADListenerGDT){
            if(!isAdvertSupport()){
                listener.onSplashRenderFail(-1,"x86 cpu is not support")
                return
            }
            mSplashADGDT = SplashAD(activity, codeId, SplashADListenerGDT(listener), 0)
            //mSplashAD!!.fetchFullScreenAdOnly()
            mSplashADGDT!!.fetchAdOnly()
        }

        @JvmStatic
        fun showSplashAd_GDT(adContainer:ViewGroup){
            //mSplashAD!!.showFullScreenAd(adContainer)
            mSplashADGDT!!.showAd(adContainer)
        }

        class SplashADListenerGDT(val listener:ISplashADListenerGDT):SplashADListener{
            override fun onADDismissed() {
                listener.onADDismissed()
            }

            override fun onNoAD(e: AdError) {
                listener.onNoAD(e.errorCode,e.errorMsg)
            }

            override fun onADPresent() {
                listener.onADPresent()
            }

            override fun onADClicked() {
                listener.onADClicked()
            }

            override fun onADTick(p0: Long) {
                listener.onADTick(p0)
            }

            override fun onADExposure() {
                listener.onADExposure()
            }

            override fun onADLoaded(p0: Long) {
                listener.onADLoaded(p0)
            }

        }

        @JvmStatic
        fun showSplashView(view:ViewGroup){
            mSplashAd?.showSplashView(view)
        }

        @JvmStatic
        fun hideSkipButton(){
            mSplashAd?.hideSkipButton()
        }

        //private var mBannerAdSlot:AdSlot? = null

        @JvmStatic
        fun loadBannerAdvert(context:Activity,codeId:String,advertWidth:Float,advertHeight:Float,isPreLoad: Boolean,listener:INativeExpressAdListener){
            if(!isAdvertSupport()){
                listener.onError(-1,"x86 cpu is not support")
                return
            }
            val ttAdNative = createAdNative(context)
            val adSlot = AdSlot.Builder()
                .setCodeId(codeId)
                .setSupportDeepLink(true)
                .setAdCount(1)
                //advert cannot autosize
                .setExpressViewAcceptedSize(advertWidth,advertHeight) //期望模板广告view的size,单位dp
                .setAdLoadType(if(isPreLoad) TTAdLoadType.PRELOAD else TTAdLoadType.LOAD) //推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .build()
            ttAdNative.loadBannerExpressAd(adSlot,TTAdNativeExpressAdListener(context,listener))
        }

        @JvmStatic
        fun loadBannerAdvert_GDT(activity: Activity,codeId:String,adContainer: ViewGroup,adWidth:Float,adHeight:Float,listener:IUnifiedBannerADListenerGDT){
            if(!isAdvertSupport()){
                listener.onError(-1,"x86 cpu is not support")
                return
            }
            val ubv = UnifiedBannerView(activity, codeId, GDTUnifiedBannerADListener(listener))
            ubv.loadAD()

            adContainer.removeAllViews()
            lateinit var lp:ViewGroup.LayoutParams
            val originLp = adContainer.layoutParams

            /* if(originLp is FrameLayout.LayoutParams){
                 lp = FrameLayout.LayoutParams(adWidth.toInt(),adHeight.toInt())
             }else if(originLp is LinearLayout.LayoutParams){
                 lp = LinearLayout.LayoutParams(adWidth.toInt(),adHeight.toInt())
             }else if(originLp is ConstraintLayout.LayoutParams){
                 lp = ConstraintLayout.LayoutParams(adWidth.toInt(),adHeight.toInt())
             }else{
                 lp = ViewGroup.LayoutParams(adWidth.toInt(),adHeight.toInt())
             }*/

            lp = originLp.apply {
                this.width = adWidth.toInt()
                this.height = adHeight.toInt()
            }

            adContainer.layoutParams = lp
            adContainer.addView(ubv)
        }

        class GDTUnifiedBannerADListener(val listener: IUnifiedBannerADListenerGDT):UnifiedBannerADListener{
            override fun onNoAD(p0: AdError) {
                listener.onNoAD(p0.errorCode,p0.errorMsg)
            }

            override fun onADReceive() {
                listener.onADReceive()
            }

            override fun onADExposure() {
                listener.onADExposure()
            }

            override fun onADClosed() {
                listener.onADClosed()
            }

            override fun onADClicked() {
                listener.onADClicked()
            }

            override fun onADLeftApplication() {
                listener.onADLeftApplication()
            }

        }

        @JvmStatic
        fun renderXxlAdView_CSJ(activity: Activity,adView:Any,listener: IAdvertRenderListener){
            (adView as TTNativeExpressAd).render()

            adView.render()
            adView.setExpressInteractionListener(object : ExpressAdInteractionListener {
                //广告点击回调
                override fun onAdClicked(view: View, type: Int) {}

                //广告展示回调
                override fun onAdShow(view: View, type: Int) {}

                //广告渲染失败回调
                override fun onRenderFail(view: View, msg: String, code: Int) {
                    listener.onRenderFail(view,msg,code)
                }

                //广告渲染成功回调
                override fun onRenderSuccess(view: View, width: Float, height: Float) {
                    listener.onRenderSuccess(view,width,height)
                }
            })

            adView!!.setDislikeCallback(activity,object : TTAdDislike.DislikeInteractionCallback{
                override fun onShow() {

                }

                override fun onSelected(position: Int, value: String, enforce: Boolean) {
                    //remove advert when user select dislike reason
                    listener.onDislikeReasonSelected(position,value,enforce)
                }

                override fun onCancel() {

                }

            })
        }

        @JvmStatic
        fun loadXxlAdvert(activity: Activity,codeId:String,advertWidth:Float,advertHeight:Float,adCount:Int,isPreLoad:Boolean,listener:INativeExpressAdListener){
            if(!isAdvertSupport()){
                listener.onError(-1,"x86 cpu is not support")
                return
            }
            val ttAdNative = TTAdSdk.getAdManager().createAdNative(activity)
            val adSlot = AdSlot.Builder()
                .setCodeId(codeId)
                .setSupportDeepLink(true)
                .setAdCount(adCount)
                //advert cannot autosize
                .setExpressViewAcceptedSize(advertWidth,advertHeight) //期望模板广告view的size,单位dp
                .setAdLoadType(if(isPreLoad) TTAdLoadType.PRELOAD else TTAdLoadType.LOAD) //推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .build()
            ttAdNative?.loadNativeExpressAd(adSlot,object:TTAdNative.NativeExpressAdListener{
                override fun onError(p0: Int, p1: String) {
                    listener.onError(p0,p1)
                }

                override fun onNativeExpressAdLoad(adViews: MutableList<TTNativeExpressAd>) {
                    if(adViews.isNullOrEmpty()) return
                    listener.onNativeExpressAdLoad(adViews)
                }

            })

        }

        //private var wrTTAdNativeExpressAd:WeakReference<Any>?=null
        //private var mTTBannerAd:TTNativeExpressAd?=null
        @JvmStatic
        fun renderBannerAdvert_CSJ(activity: Activity, adView:Any,listener: IAdvertRenderListener){
            adView as TTNativeExpressAd
            adView.render()
            adView!!.setSlideIntervalTime(10*1000)
            adView!!.setExpressInteractionListener(object : TTNativeExpressAd.AdInteractionListener{
                override fun onAdClicked(p0: View, p1: Int) {
                    listener.onAdClicked(p0,p1)
                    //ToastUtil.show("onAdClicked")
                }

                override fun onAdShow(p0: View, p1: Int) {
                    listener.onAdShow(p0,p1)
                    //ToastUtil.show("onAdShow")
                }

                override fun onRenderFail(p0: View, p1: String, p2: Int) {
                    AutoSize.autoConvertDensityOfGlobal(activity)
                    // ToastUtil.show("onRenderFail")
                    listener.onRenderFail(p0,p1,p2)
                }

                override fun onRenderSuccess(view: View, p1: Float, p2: Float) {
                    AutoSize.autoConvertDensityOfGlobal(activity)
                    listener.onRenderSuccess(view,p1,p2)
                }

                override fun onAdDismiss() {

                }

            })

            adView.setDislikeCallback(activity,object : TTAdDislike.DislikeInteractionCallback{
                override fun onShow() {

                }

                override fun onSelected(position: Int, value: String, enforce: Boolean) {
                    //remove advert when user select dislike reason
                    listener.onDislikeReasonSelected(position,value,enforce)
                }

                override fun onCancel() {

                }

            })
        }

        class TTAdNativeExpressAdListener(val activity:Activity,val listener:INativeExpressAdListener):TTAdNative.NativeExpressAdListener{
            //private var ttNativeExpressAd:TTNativeExpressAd? = null
            /*init {
                wrTTAdNativeExpressAd=WeakReference(ref)
            }*/

            override fun onError(p0: Int, p1: String) {
                //advert will modify density,so need to set back
                //val wr=wrTTAdNativeExpressAd?.get()?:return
                AutoSize.autoConvertDensityOfGlobal(activity)
                listener.onError(p0,p1)
            }

            override fun onNativeExpressAdLoad(adViews: MutableList<TTNativeExpressAd>?) {
                //val wr=wrTTAdNativeExpressAd?.get()?:return
                //advert will modify density,so need to set back
                //AutoSize.autoConvertDensityOfGlobal(bookReadActivity)
                AutoSize.autoConvertDensityOfGlobal(activity)
                if(adViews == null) return
                listener.onNativeExpressAdLoad(adViews)

                /*if(adViews==null) return
                if(adViews.size==0) return

                ttNativeExpressAd?.destroy()
                ttNativeExpressAd=adViews[0]
                ttNativeExpressAd!!.render()
                ttNativeExpressAd!!.setSlideIntervalTime(10*1000)
                ttNativeExpressAd!!.setExpressInteractionListener(object : TTNativeExpressAd.AdInteractionListener{
                    override fun onAdClicked(p0: View, p1: Int) {
                        listener.onAdClicked(p0,p1)
                        //ToastUtil.show("onAdClicked")
                    }

                    override fun onAdShow(p0: View, p1: Int) {
                        listener.onAdShow(p0,p1)
                        //ToastUtil.show("onAdShow")
                    }

                    override fun onRenderFail(p0: View, p1: String, p2: Int) {
                        // ToastUtil.show("onRenderFail")
                        listener.onRenderFail(p0,p1,p2)
                    }

                    override fun onRenderSuccess(view: View, p1: Float, p2: Float) {

                        listener.onRenderSuccess(view,p1,p2)
                    }

                    override fun onAdDismiss() {

                    }

                })

                ttNativeExpressAd!!.setDislikeCallback(context as Activity,object : TTAdDislike.DislikeInteractionCallback{
                    override fun onShow() {

                    }

                    override fun onSelected(position: Int, value: String, enforce: Boolean) {
                        //remove advert when user select dislike reason
                        listener.onDislikeReasonSelected(position,value,enforce)
                    }

                    override fun onCancel() {

                    }

                })*/

            }

        }

        //private var mRewardAdSlot:AdSlot? = null

        @JvmStatic
        fun loadRewardAdvert(context:Context,codeId:String,preload: Boolean,listener:IRewardVideoAdListener){
            if(!isAdvertSupport()){
                listener.onVideoError()
                return
            }
            //if(mTTAdNative==null)
            val ttAdNative = TTAdSdk.getAdManager().createAdNative(context)
            //if(mRewardAdSlot==null)
            val rewardAdSlot = AdSlot.Builder()
                .setCodeId(codeId)
                .setAdLoadType(if(preload) TTAdLoadType.PRELOAD else TTAdLoadType.LOAD)
                .build()
            ttAdNative.loadRewardVideoAd(rewardAdSlot,TTAdNativeRewardVideoAdListener(listener))
        }


        //private var wrTTAdNativeReward:WeakReference<Any>?=null
        private var mTTRewardVideoAd: TTRewardVideoAd?=null

        class TTAdNativeRewardVideoAdListener(val listener:IRewardVideoAdListener):TTAdNative.RewardVideoAdListener{
            init{
                //wrTTAdNativeReward=WeakReference<Any>(ref)
            }

            override fun onError(p0: Int, p1: String) {
                //val wr=wrTTAdNativeReward?.get()?:return
                listener.onError(p0,p1)
            }

            override fun onRewardVideoAdLoad(ad: TTRewardVideoAd?) {
                //show
            }

            //deprecated
            override fun onRewardVideoCached() {

            }

            override fun onRewardVideoCached(ad: TTRewardVideoAd?) {
                //render
                //val wr=wrTTAdNativeReward?.get()?:return

                mTTRewardVideoAd = ad
                listener.onRewardVideoCached()

                mTTRewardVideoAd!!.setRewardAdInteractionListener(object: TTRewardVideoAd.RewardAdInteractionListener{
                    override fun onAdShow() {

                    }

                    override fun onAdVideoBarClick() {

                    }

                    override fun onAdClose() {
                        listener.onClose()
                    }

                    override fun onVideoComplete() {

                    }

                    override fun onVideoError() {
                        listener.onVideoError()
                    }

                    //deprecated
                    override fun onRewardVerify(
                        p0: Boolean,
                        p1: Int,
                        p2: String?,
                        p3: Int,
                        p4: String?
                    ) {

                    }

                    override fun onRewardArrived(isRewardValid: Boolean, rewardType: Int, bundle: Bundle?) {
                        //Bundle[{reward_extra_key_error_code=0, reward_extra_key_is_server_verify=false, reward_extra_key_has_video_complete_reward=false, reward_extra_key_reward_name=, reward_extra_key_reward_propose=1.0, reward_extra_key_error_msg=null, reward_extra_key_reward_amount=0}]
                        listener.onRewardArrived(isRewardValid,rewardType,bundle)
                    }

                    override fun onSkippedVideo() {

                    }

                })

            }
        }

        @JvmStatic
        fun showRewardVideoAd(activity:Activity){
            mTTRewardVideoAd?.showRewardVideoAd(activity)
        }

        var mRewardVideoADGDT:RewardVideoAD? = null

        @JvmStatic
        fun loadRewardAdvert_GDT(context:Context,codeId:String,listener:IRewardVideoADListenerGDT){
            if(!isAdvertSupport()){
                listener.onError(-1,"x86 cpu is not support")
                return
            }
            mRewardVideoADGDT = RewardVideoAD(context,codeId,GDTRewardVideoADListener(listener))
            mRewardVideoADGDT!!.loadAD()
        }

        @JvmStatic
        fun showRewardAdvert_GDT(){
            if(mRewardVideoADGDT == null || !mRewardVideoADGDT!!.isValid) return
            mRewardVideoADGDT!!.showAD()
        }

        private var mAdManagerGDT:NativeUnifiedAD? = null

        @JvmStatic
        fun loadCustomBannerAdvert_GDT(context: Context,codeId: String, containerView:NativeAdContainer,clickView:View,listener:IGDTNativeADUnifiedListener){
            if(!isAdvertSupport()){
                listener.onNoAD(-1,"x86 cpu is not support")
                return
            }
            mAdManagerGDT = NativeUnifiedAD(context, codeId, GDTNativeADUnifiedListener(context,containerView,clickView,listener))
            mAdManagerGDT!!.loadData(1)

        }

        class GDTNativeADUnifiedListener(val context:Context,val containerView:NativeAdContainer,val clickView:View, val listener:IGDTNativeADUnifiedListener):NativeADUnifiedListener{
            override fun onNoAD(p0: AdError) {
                listener.onNoAD(p0.errorCode,p0.errorMsg)
            }

            override fun onADLoaded(adViews: MutableList<NativeUnifiedADData>?) {
                if(adViews == null || adViews!!.isEmpty()) return
                val ad = adViews[0]
                ad.bindAdToView(context,containerView,null, listOf(clickView))
                listener.onADLoaded(ad.iconUrl,ad.title,ad.desc)
            }

        }

        class GDTRewardVideoADListener(val listener:IRewardVideoADListenerGDT):RewardVideoADListener{
            override fun onADLoad() {
                listener.onADLoad()
            }

            override fun onVideoCached() {
                listener.onVideoCached()
            }

            override fun onADShow() {
                listener.onADShow()
            }

            override fun onADExpose() {
                listener.onADExpose()
            }

            override fun onReward(p0: MutableMap<String, Any>) {
                listener.onReward(p0)
            }

            override fun onADClick() {
                listener.onADClick()
            }

            override fun onVideoComplete() {
                listener.onVideoComplete()
            }

            override fun onADClose() {
                listener.onADClose()
            }

            override fun onError(e: AdError) {
                listener.onError(e.errorCode,e.errorMsg)
            }

        }

        @JvmStatic
        fun addBannerAdvertCloseBtn(context:Context,cardContainer:ViewGroup?,adContainer:ViewGroup?,view:View?,isAddCloseBtn:Boolean){
            ViewHelper.setVisibility(true,adContainer)
            if(view != null){
                val lp = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)
                lp.gravity = Gravity.BOTTOM
                view.layoutParams = lp
            }
            if(view != null){
                adContainer?.removeAllViews()
                adContainer?.addView(view)
                adContainer?.post{
                    if(adContainer == null) return@post
                    if(!isAddCloseBtn) return@post

                    val childCount = 3
                    if(adContainer!!.childCount >= childCount) return@post

                    val closeBgView = View(context)
                    val closeBgLp = FrameLayout.LayoutParams(44.dp2px(),adContainer.height)
                    closeBgLp.gravity = Gravity.RIGHT
                    closeBgView.layoutParams = closeBgLp
                    closeBgView.setBackgroundColor(android.graphics.Color.TRANSPARENT)

                    val closeView = ImageView(context)
                    closeView.scaleType = ImageView.ScaleType.CENTER_CROP
                    closeView.setImageResource(com.songcha.library_business.R.mipmap.business_icon_advert_close_gray)
                    val closeLp = FrameLayout.LayoutParams(24.dp2px(),24.dp2px())
                    closeLp.gravity = Gravity.RIGHT or Gravity.CENTER_VERTICAL
                    closeLp.rightMargin = 10.dp2px()
                    closeView.layoutParams = closeLp
                    closeView.setOnClickListener {
                        adContainer?.removeAllViews()
                        if(cardContainer != null)
                            ViewHelper.setVisibility(false,cardContainer)
                    }

                    adContainer?.addView(closeBgView)
                    adContainer?.addView(closeView)
                }

            }

            if(cardContainer != null)
                ViewHelper.setVisibility(true,cardContainer)
        }

        @JvmStatic
        fun addXxlAdvertCloseBtn(context: Context,isCsjAdvert:Boolean,cardContainer: ViewGroup?, adContainer:ViewGroup,view:View){
            if(view==null) return

            ViewHelper.setVisibility(true,adContainer)

            val lp = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)
            lp.gravity = Gravity.BOTTOM
            view.layoutParams = lp
            adContainer.removeAllViews()
            adContainer.addView(view)

            //gdt top text,bottom pic
            //csj top pic,bottom text
            val closeView = ImageView(context)
            closeView.scaleType = ImageView.ScaleType.CENTER_CROP
            closeView.setImageResource(com.songcha.library_business.R.mipmap.business_icon_advert_close)
            val closeLp = FrameLayout.LayoutParams(24.dp2px(),24.dp2px())
            closeLp.gravity = Gravity.RIGHT
            closeLp.topMargin = if(isCsjAdvert) 20.dp2px() else 80.dp2px()
            closeLp.rightMargin = 20.dp2px()
            closeView.layoutParams = closeLp
            closeView.setOnClickListener {
                adContainer.removeAllViews()
                if(cardContainer != null)
                    ViewHelper.setVisibility(false,cardContainer)
            }
            adContainer.addView(closeView)
            if(cardContainer != null)
                ViewHelper.setVisibility(true,cardContainer)
        }

        @JvmStatic
        fun loadInsertScreenAdvert(activity:Activity,codeId:String,isPreLoad: Boolean,listener:IInsertScreenAdListener){
            if(!isAdvertSupport()){
                listener.onError(-1,"x86 cpu is not support")
                return
            }
            val mTTAdNative = TTAdSdk.getAdManager().createAdNative(activity)
            val adSlot = AdSlot.Builder()
                .setCodeId(codeId)
                .setSupportDeepLink(true)
                .setAdLoadType(if(isPreLoad) TTAdLoadType.PRELOAD else TTAdLoadType.LOAD)
                .build()
            mTTAdNative.loadFullScreenVideoAd(adSlot,object: TTAdNative.FullScreenVideoAdListener {
                override fun onError(p0: Int, p1: String?) {
                    AutoSize.autoConvertDensityOfGlobal(activity)
                    listener.onError(p0,p1.toString())
                }

                override fun onFullScreenVideoAdLoad(p0: TTFullScreenVideoAd?) {

                }

                override fun onFullScreenVideoCached() {

                }

                override fun onFullScreenVideoCached(ad: TTFullScreenVideoAd?) {
                    AutoSize.autoConvertDensityOfGlobal(activity)

                    if(ad == null) return
                    ad.showFullScreenVideoAd(activity,TTAdConstant.RitScenes.GAME_GIFT_BONUS,null)
                    ad.setFullScreenVideoAdInteractionListener(object:FullScreenVideoAdInteractionListener{
                        override fun onAdShow() {
                            listener.onSuccess()
                        }

                        override fun onAdVideoBarClick() {

                        }

                        override fun onAdClose() {
                            listener.onAdClose()
                        }

                        override fun onVideoComplete() {

                        }

                        override fun onSkippedVideo() {

                        }

                    })
                }

            })

        }

        @JvmStatic
        fun loadInsertScreenAdvert_GDT(activity:Activity,codeId:String,isFullScreen:Boolean,listener:IInsertScreenAdListener){
            var uiad:UnifiedInterstitialAD? = null
            uiad = UnifiedInterstitialAD(activity,codeId,object:UnifiedInterstitialADListener{
                override fun onADReceive() {

                }

                override fun onVideoCached() {

                }

                override fun onNoAD(p0: AdError?) {
                    listener.onError(-1,"no ad")
                }

                override fun onADOpened() {

                }

                override fun onADExposure() {

                }

                override fun onADClicked() {

                }

                override fun onADLeftApplication() {

                }

                override fun onADClosed() {
                    listener.onAdClose()
                }

                override fun onRenderSuccess() {
                    //TODO
                    if(isFullScreen){
                        uiad!!.showFullScreenAD(activity)
                    }else{
                        uiad!!.show()
                    }

                    listener.onSuccess()
                }

                override fun onRenderFail() {
                    listener.onError(-1,"render fail")
                }

            })

            if(isFullScreen){
                uiad!!.loadFullScreenAD()
            }else{
                uiad!!.loadAD()
            }

        }

        @JvmStatic
        fun loadBannerAdvertCommon_CSJ(activity: Activity,codeId:String,margin:Float,
                                       adContainer:FrameLayout,
                                       ar:Float,
                                       isPreLoad: Boolean,
                                       listener: ILoadAdvertCommonListener){
            val initDensity = AutoSizeConfig.getInstance().initDensity
            val adWidth = (ScreenUtil.getScreenWidth() - margin.dp2px()) / initDensity
            val adHeight = if(ar > 0f) adWidth / ar else 0f

            loadBannerAdvert(activity,
                codeId,adWidth,adHeight,isPreLoad,object:INativeExpressAdListener{
                    override fun onError(code: Int, msg: String) {
                        adContainer.removeAllViews()
                        ViewHelper.setVisibility(false,adContainer)
                        listener.onError(AdvertInfoBean.ADVERT_TYPE_CSJ,code,msg)
                    }

                    override fun onNativeExpressAdLoad(adViews:List<Any>) {
                        renderBannerAdvert_CSJ(activity,adViews[0],object :IAdvertRenderListener{
                            override fun onAdClicked(view: View, p1: Int) {

                            }

                            override fun onAdShow(view: View, p1: Int) {

                            }

                            override fun onRenderFail(p0: View, p1: String, p2: Int) {
                                adContainer.removeAllViews()
                                ViewHelper.setVisibility(false,adContainer)
                                listener.onError(AdvertInfoBean.ADVERT_TYPE_CSJ,p2,p1)
                            }

                            override fun onRenderSuccess(view: View, p1: Float, p2: Float) {
                                adContainer.removeAllViews()
                                ViewHelper.setVisibility(true,adContainer)
                                adContainer.addView(view)
                                listener.onSuccess(adHeight.toInt(),AdvertInfoBean.ADVERT_TYPE_CSJ,null)
                            }

                            override fun onDislikeReasonSelected(
                                position: Int,
                                value: String,
                                enforce: Boolean
                            ) {
                                adContainer.removeAllViews()
                                ViewHelper.setVisibility(false,adContainer)
                                listener.onClose(AdvertInfoBean.ADVERT_TYPE_CSJ)
                            }

                        })
                    }

                })

        }

        @JvmStatic
        fun loadBannerAdvertCommon_GDT(activity: Activity,codeId:String,margin:Float,adContainer:FrameLayout,
                                       ar:Float,
                                       listener:ILoadAdvertCommonListener){
            val adWidth = (ScreenUtil.getScreenWidth() - margin.dp2px())
            val adHeight = if(ar > 0f) adWidth / ar else 0f

            loadBannerAdvert_GDT(activity,codeId,adContainer,adWidth,adHeight,object:IUnifiedBannerADListenerGDT{
                override fun onError(code: Int, msg: String) {
                    adContainer.removeAllViews()
                    ViewHelper.setVisibility(false,adContainer)
                    listener.onError(AdvertInfoBean.ADVERT_TYPE_GDT,code,msg)
                }

                override fun onNoAD(code: Int, msg: String) {
                    adContainer.removeAllViews()
                    ViewHelper.setVisibility(false,adContainer)
                    listener.onError(AdvertInfoBean.ADVERT_TYPE_GDT,code,msg)
                }

                override fun onADReceive() {
                    //adContainer.removeAllViews()
                    ViewHelper.setVisibility(true,adContainer)
                    listener.onSuccess(adHeight.toInt(),AdvertInfoBean.ADVERT_TYPE_GDT,null)
                }

                override fun onADExposure() {

                }

                override fun onADClosed() {
                    adContainer.removeAllViews()
                    ViewHelper.setVisibility(false,adContainer)
                    listener.onClose(AdvertInfoBean.ADVERT_TYPE_GDT)
                }

                override fun onADClicked() {

                }

                override fun onADLeftApplication() {

                }

            })
        }

        //TODO
        @JvmStatic
        fun loadXxlAdvertCommon_CSJ(activity: Activity,codeId:String,margin:Float,adContainer:FrameLayout,
                                    onError:(()->Unit)? = null){
            val initDensity = AutoSizeConfig.getInstance().initDensity
            val adWidth = (ScreenUtil.getScreenWidth() - margin.dp2px()) / initDensity

            loadXxlAdvert(activity,codeId,adWidth,0f,1, true,object:INativeExpressAdListener{
                override fun onError(code: Int, msg: String) {
                    adContainer.removeAllViews()
                    ViewHelper.setVisibility(false,adContainer)
                    onError?.invoke()
                }

                override fun onNativeExpressAdLoad(adViews:List<Any>) {
                    AdvertHelper.renderXxlAdView_CSJ(activity,adViews[0],object :IAdvertRenderListener{
                        override fun onAdClicked(view: View, p1: Int) {

                        }

                        override fun onAdShow(view: View, p1: Int) {

                        }

                        override fun onRenderFail(p0: View, p1: String, p2: Int) {
                            adContainer.removeAllViews()
                            ViewHelper.setVisibility(false,adContainer)
                            onError?.invoke()
                        }

                        override fun onRenderSuccess(view: View, p1: Float, p2: Float) {
                            adContainer.removeAllViews()
                            ViewHelper.setVisibility(true,adContainer)
                            adContainer.addView(view)
                        }

                        override fun onDislikeReasonSelected(
                            position: Int,
                            value: String,
                            enforce: Boolean
                        ) {
                            adContainer.removeAllViews()
                            ViewHelper.setVisibility(false,adContainer)
                        }

                    })
                }

            })
        }

        @JvmStatic
        fun loadXxlAdvertCommon_GDT(activity: Activity,codeId:String,margin:Float,adContainer:FrameLayout,
                                    onError:(()->Unit)? = null){
            val initDensity = AutoSizeConfig.getInstance().initDensity
            val density = ScreenUtil.getDensity()
            val scale = density / initDensity
            //val adWidth = ((360 - margin) * scale).toInt()
            val adWidth = ((ScreenUtil.getScreenWidth() - margin.dp2px()) * scale).px2dp().toInt()

            //TODO
            loadXxlAdvert_GDT(activity,codeId,adWidth,1,object:IGDTNativeExpressADListener{
                override fun onNoAD(code: Int, msg: String) {
                    adContainer.removeAllViews()
                    ViewHelper.setVisibility(false,adContainer)
                    onError?.invoke()
                }

                override fun onADLoaded(adViews: List<View>) {
                    renderXxlAdView_GDT(adViews[0])
                }

                override fun onRenderFail(adView: View) {
                    adContainer.removeAllViews()
                    ViewHelper.setVisibility(false,adContainer)
                    onError?.invoke()
                }

                override fun onRenderSuccess(adView: View) {
                    adContainer.removeAllViews()
                    adContainer.addView(adView)
                    ViewHelper.setVisibility(true,adContainer)
                }

                override fun onADExposure(adView: View) {

                }

                override fun onADClicked(adView: View) {

                }

                override fun onADClosed(adView: View) {
                    adContainer.removeAllViews()
                    ViewHelper.setVisibility(false,adContainer)
                }

                override fun onADLeftApplication(adView: View) {

                }

            })
        }

        @JvmStatic
        fun loadInsertScreenAdvertCommon_CSJ(activity: Activity,codeId:String,
                                             onSuccess:(()->Unit)? = null,onError:(()->Unit)? = null,onClose:(()->Unit)? = null){
            loadInsertScreenAdvert(activity,codeId,true,object:IInsertScreenAdListener{
                override fun onError(code: Int, msg: String) {
                    onError?.invoke()
                }

                override fun onSuccess() {

                }

                override fun onAdShow() {
                    onSuccess?.invoke()
                }

                override fun onAdClose() {
                    onClose?.invoke()
                }

            })
        }

        @JvmStatic
        fun loadInsertScreenAdvertCommon_GDT(activity: Activity,codeId:String,isFullScreen: Boolean,
                                             onSuccess:(()->Unit)? = null,onError:(()->Unit)? = null,onClose:(()->Unit)? = null){
            loadInsertScreenAdvert_GDT(activity,codeId,isFullScreen,object:IInsertScreenAdListener{
                override fun onError(code: Int, msg: String) {
                    onError?.invoke()
                }

                override fun onSuccess() {
                    onSuccess?.invoke()
                }

                override fun onAdShow() {

                }

                override fun onAdClose() {
                    onClose?.invoke()
                }

            })
        }

        @JvmStatic
        fun loadRewardAdvertCommon_CSJ(activity: Activity,codeId:String,onSuccess:(()->Unit)? = null,onError:(()->Unit)? = null){
            loadRewardAdvert(activity,codeId,true,object:IRewardVideoAdListener{
                override fun onError(code: Int, msg: String) {
                    onError?.invoke()
                }

                override fun onVideoError() {
                    onError?.invoke()
                }

                override fun onRewardArrived(
                    isRewardValid: Boolean,
                    rewardType: Int,
                    bundle: Bundle?
                ) {
                    onSuccess?.invoke()
                }

                override fun onRewardVideoCached() {
                    showRewardVideoAd(activity)
                }

                override fun onClose() {

                }

            })
        }

        @JvmStatic
        fun loadRewardAdvertCommon_GDT(activity: Activity,codeId:String,onSuccess:(()->Unit)? = null,onError:(()->Unit)? = null){
            loadRewardAdvert_GDT(activity,codeId,object:IRewardVideoADListenerGDT{
                override fun onADLoad() {
                    showRewardAdvert_GDT()
                }

                override fun onVideoCached() {

                }

                override fun onADShow() {

                }

                override fun onADExpose() {

                }

                override fun onReward(p0: MutableMap<String, Any>) {
                    onSuccess?.invoke()
                }

                override fun onADClick() {

                }

                override fun onVideoComplete() {

                }

                override fun onADClose() {

                }

                override fun onError(code: Int, msg: String) {
                    onError?.invoke()
                }

            })
        }


        @JvmStatic
        private fun isAdvertSupport():Boolean{
            //CPU_ABI is v7a when run on app,module is x86
            //TODO
            //return true
            return !(BuildConfig.DEBUG && Build.CPU_ABI == "x86")
        }

        @JvmStatic
        fun destroy(){
            //mTTAdNative = null
            //mSplashAd = null
            //mBannerAdSlot = null
            //mFeedAdSlot = null
            //mTTFeedAd = null
            //mRewardAdSlot = null
            //mTTBannerAd?.destroy()
            //mTTBannerAd = null
            mTTRewardVideoAd = null
        }

    }
}